/**
 * Copyright (c) 2011-2019, James Zhan 詹波 (jfinal@126.com).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.jfinal.plugin.activerecord.generator;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * TableMeta
 */
@SuppressWarnings("serial")
public class TableMeta implements Serializable {

    public String db;                   // 库名
    public String name;					// 表名
	public String remarks;				// 表备注
	public String primaryKey;			// 主键，复合主键以逗号分隔
	public List<ColumnMeta> columnMetas = new ArrayList<ColumnMeta>();	// 字段 meta

	// ---------

	public String baseModelName;		// 生成的 base model 名
	public String baseModelContent;		// 生成的 base model 内容

	public String modelName;			// 生成的 model 名
	public String modelContent;			// 生成的 model 内容

	// ---------

	public int colNameMaxLen = "Field".length();			// 字段名最大宽度，用于辅助生成字典文件样式
	public int colTypeMaxLen = "Type".length();				// 字段类型最大宽度，用于辅助生成字典文件样式
	public int colDefaultValueMaxLen = "Default".length();	// 字段默认值最大宽度，用于辅助生成字典文件样式

    //======================================================================
    public List<String> modules() {
        List<String> modules = new ArrayList<>();
        String[] arr = name.split("__");
        if (arr.length > 1) {
            for (int i = 0; i < arr.length - 1; i++) {
                modules.add(arr[i]);
            }
        }
        return modules;
    }

    public String getModulePackageName() {
        if (this.modules().isEmpty()) {
            return "";
        }
        return "." + String.join(".", this.modules());
    }

    public String getModuleFilePath() {
        if (this.modules().isEmpty()) {
            return "";
        }
        return "/" + String.join("/", this.modules());
    }

    public static void main(String[] args) {
        String s = "sys_user_role";
        String[] ss = s.split("__");
        System.out.println(ss.length);

        String s2 = "sys__user_role";
        String[] ss2 = s2.split("__");
        System.out.println(ss2.length);

        String s3 = "erp__sys__user_role";
        String[] ss3 = s3.split("__");
        System.out.println(ss3.length);

        String modulePath = "";
        for (int i = 0; i < ss3.length - 1; i++) {
            modulePath += "/" + ss3[i];
        }
        System.out.println(modulePath);

        TableMeta tableMeta = new TableMeta();
        tableMeta.name = "erp__sys__user_role";
        String path = String.join("/", tableMeta.modules());
        System.out.println(path);

    }
}




